package com.tweetgraph.domain;

import java.io.Serializable;
import java.util.Set;
import java.util.TreeSet;

public class ResultAnalysis implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2792532983824935806L;

	public static class Terms implements Serializable,Comparable<Terms>{

		/**
		 * 
		 */
		private static final long serialVersionUID = 2796447598483059540L;
		
		public Terms(String aterm){
			this.term = aterm;
		}
		
		
		private String term;
		private int count;
		public String getTerm() {
			return term;
		}
		public void setTerm(String term) {
			this.term = term;
		}
		public int getCount() {
			return count;
		}
		public void setCount(int count) {
			this.count = count;
		}
		
		public void  incrCount(){
			this.count++;
		}
		
		public int compareTo(Terms obj){
			if(obj != null){
				return this.term.compareTo(obj.term);
			}else{
				return 1;
			}
		}
		
		public String toString(){
			return "\tTerm:"+term+"\t count:"+count+"\n";  
		}
		
		public String toJSONString(){
			return "\""+term+"\":\""+count+"\"";
		}
		
	}
	 
	private Set<Terms> termSet = new TreeSet<Terms>();
	private String searchTerm;
	
	/**
	 * Constructor takes in the searchTerm
	 * @param asearchTerm
	 */
	public ResultAnalysis(String asearchTerm){
		this.searchTerm=asearchTerm;
	}
	
	public void addTerm(Terms term){
		this.termSet.add(term);
	}
	
	public Set<Terms> getTermSet(){
		return this.termSet;
	}
	
	private int otherCount = 0;
	
	private boolean doCountOther = false;
	
	public void incrementOtherCount(){
		otherCount++;
	}
	
	
	
	public boolean isDoCountOther() {
		return doCountOther;
	}

	public void setDoCountOther(boolean doCountOther) {
		this.doCountOther = doCountOther;
	}

	public int getOtherCount() {
		return otherCount;
	}

	/**
	 * 
	 */
	public String toString(){
		StringBuilder bldr = new StringBuilder();
		for(Terms term:termSet){
			bldr.append(term.toString());
		}
		return bldr.toString();
	}
	
	public String toJSONString(){
		StringBuilder bldr = new StringBuilder();
		int count = 0;
		
		bldr.append("{\n");
		for(Terms term:termSet){
			bldr.append(term.toJSONString());
			if(++count < termSet.size() || doCountOther){
				bldr.append(",\n");
			}
		}
		if(doCountOther){
			bldr.append("\"Others\":\""+otherCount+"\"\n");
		}
		
		bldr.append("\n}");
		return bldr.toString();
	}
}
